package com.business.yang.mvp.view.my.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.business.yang.R;
import com.business.yang.core.BaseMainActivity;
import com.business.yang.mvp.model.bean.MyBean;
import com.business.yang.mvp.presenter.my.MyPresenter;
import com.business.yang.mvp.view.my.view.IMyParentView;
import com.lyxs916.adapter.ViewHolder;
import com.lyxs916.adapter.recyclerview.CommonAdapter;
import com.lyxs916.xrecycleview.SuperRecyclerView;
import com.lyxs916.utilslibrary.ui.ToastUtils;
import com.lyxs916.utilslibrary.ui.divider.HorizontalDividerItemDecoration;
import com.lyxs916.utilslibrary.view.SupperToolBar;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import butterknife.BindView;

public class MyActivity extends BaseMainActivity<IMyParentView.IMyView, MyPresenter> implements IMyParentView.IMyView {
    @BindView(R.id.toolbar)
    SupperToolBar toolbar;

    @BindView(R.id.superRecyclerView)
    SuperRecyclerView switchRootId;
    ArrayList<MyBean> mDatas = new ArrayList<>();
    CommonAdapter adapter;

    @BindView(R.id.rootView)
    ViewGroup miewGroup;

    public static void startUi(Context context) {
        Intent intent = new Intent(context, MyActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int setContentView() {
        return R.layout.activity_my;
    }

    @Override
    public void parseIntent(Intent intent) {

    }

    @Override
    public int currentItem() {
        return 2;
    }

    @Override
    public void initView() {
        super.initView();
        toolbar.setTitle("我的");

        findViewById(R.id.refresh).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.requestHttpData(true, miewGroup);
            }
        });

        switchRootId.setOnLoaddingListener(this);
        switchRootId.setOnRefreshListener(this);
        switchRootId.getRecyclerView().setLayoutManager(new LinearLayoutManager(this));
        switchRootId.getRecyclerView().addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).sizeResId(R.dimen.dp_5).colorResId(R.color.gray_ee).build());
        switchRootId.setAdapter(adapter = new CommonAdapter<MyBean>(this, R.layout.test_item_test, mDatas) {
            @Override
            public void convert(ViewHolder holder, MyBean s) {
                holder.setText(R.id.test, "第" + holder.getmPosition() + "条： " + s.getHourNumber());
            }
        });
        presenter.requestHttpData(true, miewGroup);
    }


    @Override
    public MyPresenter initPresenter() {
        return new MyPresenter();
    }

    @Override
    public void resultData(List<MyBean> data) {
        mDatas.addAll(data);
        adapter.notifyDataSetChanged();

    }

    @Override
    public void coreOnLoadingBarListener(View parent) {
        ToastUtils.getToastLong("123456");
        presenter.requestHttpData(true, miewGroup);
    }

    @Override
    public SuperRecyclerView getSuperRecyclerView() {
        return switchRootId;
    }

    @Override
    public void onRefresh() {
//        mDatas.clear();
        adapter.notifyDataSetChanged();
        presenter.requestHttpData(true, miewGroup);
    }

    @Override
    public void onLoadding() {
        presenter.requestHttpData(false, miewGroup);
    }

    @Override
    public void clearData() {
        mDatas.clear();
    }


    @Override
    public <T> Collection<T> getValidData(Collection<T> c) {
        return switchRootId.getPagingHelp().getValidData(c);
    }

    @Override
    public void clearPagingData() {
        switchRootId.getPagingHelp().clear();
    }

    @Override
    public int getPageindex() {
        return switchRootId.getPagingHelp().getPageindex();
    }

    @Override
    public int getPageCount() {
        return switchRootId.getPagingHelp().getPageCount();
    }

}
